//
//  HomeDetialViewController.m
//  YiKao2
//
//  Created by Mac Jc on 15/8/1.
//  Copyright (c) 2015年 Mac Jc. All rights reserved.
//

#import "HomeDetialViewController.h"
#import "AllHeader.h"
#import "HomeNavigationVIew.h"
#import "HomeDetialHeaderVIew.h"
#import "CommentTableViewCell.h"
//#import "TweetDetailCommentCell.h"
//#import "UIMessageInputView.h"
//#import "Comment.h"

//#define commentString @"这是评论文字，用来测试用户发表的评论，文字稍多，测试自动换行和测试label高度大小"

@interface HomeDetialViewController ()<UITableViewDelegate,UITableViewDataSource>

// 滑动视图
@property (nonatomic ,retain) UITableView *tableView;

// 图片视图
//@property (nonatomic,retain) UIImageView *backgroundImageView;

// 图片高度
//@property (nonatomic, assign) CGFloat imageViewHeight;

// 文字
//@property (nonatomic,retain) UIView *textView;

@property (nonatomic,retain) HomeNavigationVIew *navigationView;

@property (nonatomic,retain) HomeDetialHeaderVIew *headerView;



@end

@implementation HomeDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kBackgroundColor;
    self.navigationController.navigationBar.hidden = YES;
//    [self prefersStatusBarHidden];
    
    [self createTableView];
    

    [self createNaviagtionView];

    
    
    
}

- (void)createTableView{

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44,SCREEN_WIDTH,SCREEN_HEIGHT)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = kCustomColor(235, 235, 235, 1);
    _tableView.delegate=self;
    _tableView.dataSource=self;

    CGFloat imageViewHeight = _backgroundImage.size.height / _backgroundImage.size.width * (SCREEN_WIDTH - 20 * WIDTH);
    CGFloat describeLabelHeight = [UILabel heightWithString:_describeString theWidth:SCREEN_WIDTH - 60 font:[UIFont systemFontOfSize:16]];

    //  重新设定数据
    _headerView = [[HomeDetialHeaderVIew alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, imageViewHeight + 160 + describeLabelHeight)];
    _headerView.backgroundImageView.image = _backgroundImage;
    _headerView.iconView.image = _iconImage;
    _headerView.tagLabel.text = _tagString;
    _headerView.authorLabel.text = _authorString;
    _headerView.describeLabel.text = _describeString;
    [UILabel setLineSpacingWithLabel:_headerView.describeLabel withLineSpacing:4.5];
    _headerView.viewNumLabel.text = _viewNumString;
    _headerView.goodLabel.text = _goodNumString;
    _headerView.commentNumLabel.text = [NSString stringWithFormat:@"评论(%@)",_commentNumString];

    //  重新设置尺寸
    _headerView.shadowView.frame = CGRectMake(0, -SCREEN_HEIGHT, SCREEN_WIDTH, _headerView.bounds.size.height - 35 + SCREEN_HEIGHT);
    _headerView.backgroundImageView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, imageViewHeight);
    _headerView.iconView.frame = CGRectMake(10 * WIDTH, _headerView.backgroundImageView.bottom + 20, 30, 30);
    _headerView.tagLabel.frame = CGRectMake(50 * WIDTH, _headerView.backgroundImageView.bottom + 22, 100 * WIDTH, 14 * HEIGHT);
    _headerView.authorLabel.frame = CGRectMake(50 * WIDTH, _headerView.tagLabel.bottom, 100 * WIDTH, 14 * HEIGHT);
    
    _headerView.collectionButton.frame = CGRectMake(SCREEN_WIDTH - 80 - 10, _headerView.backgroundImageView.bottom + 20, 80 * WIDTH, 30 * HEIGHT);

    _headerView.describeLabel.frame = CGRectMake(10 * WIDTH, _headerView.collectionButton.bottom + 15, SCREEN_WIDTH - 50 , describeLabelHeight);
    
    _headerView.viewNumImageView.frame = CGRectMake(SCREEN_WIDTH - 100 * WIDTH, _headerView.shadowView.bottom - 25, 15, 12);
    _headerView.viewNumLabel.frame = CGRectMake(_headerView.viewNumImageView.right + 3, _headerView.viewNumImageView.top, 25, 12);
    _headerView.goodNumImageView.frame = CGRectMake(_headerView.viewNumLabel.right + 5, _headerView.viewNumImageView.top, 15, 12);
    _headerView.goodLabel.frame = CGRectMake(_headerView.goodNumImageView.right + 3, _headerView.viewNumLabel.top, 25, 12);
    
    _headerView.commentNumLabel.frame = CGRectMake(10 * WIDTH, _headerView.bottom - 25, 100, 17);
    _headerView.lineView.frame = CGRectMake(0, _headerView.bottom - 0.5, SCREEN_WIDTH, 0.5);
    
    _tableView.tableHeaderView = _headerView;
    
    [self.view addSubview:_tableView];
}

//  HeadView **********************************************

- (void)createNaviagtionView{
    _navigationView = [[HomeNavigationVIew alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [self.view addSubview:_navigationView];
    
    [_navigationView.backButton addTarget:self action:@selector(backBefore) forControlEvents:(UIControlEventTouchUpInside)];
    _navigationView.titleLabel.text = _authorString;
    
}

// 返回上一层
- (void)backBefore{
//    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];

}


#pragma mark - barHidden**************************
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


#pragma mark - UITableViewDataSource**************************

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//返回行数，也就是返回数组中所存储数据，也就是section的元素
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10 + 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[CommentTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier];
    }
    _commentContentString = @"这是评论文字，用来测试用户发表的评论，文字稍多，测试自动换行和测试label高度大小";
    cell.backgroundColor = kCustomColor(235, 235, 235, 1);
    cell.nameLabel.text = @"评论者姓名";
    cell.commentContentLabel.text = _commentContentString;
    CGFloat commentContentLaebleHeight = [UILabel heightWithString:_commentContentString theWidth:280 font:[UIFont systemFontOfSize:15.5]];
    [UILabel setLineSpacingWithLabel:cell.commentContentLabel withLineSpacing:3.5];
    cell.commentContentLabel.frame = CGRectMake(55 , 30, 280, commentContentLaebleHeight);

    return cell;
}



//设置单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    CGFloat commentLabelHeight = [UILabel heightWithString:_commentContentString theWidth:280 font:[UIFont systemFontOfSize:15.5]];
    
    
    return cell.frame.size.height + commentLabelHeight;
}

//选中单元格所产生事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *message = [[NSString alloc]initWithFormat:@"You selected%ld",(long)indexPath.row];
    //    弹出警告信息
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                   message:message
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles: nil];
    [alert show];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
